﻿using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace Snefru.CustomComponent
{
    public class SnefruProgressBar : ProgressBar
    {
        public string LabelText { get; set; }
        public bool IsShowValue { get; set; }

        public SnefruProgressBar()
            : base()
        {
            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.Paint += OnLabelPaint;
        }

        public void OnLabelPaint(object sender, PaintEventArgs e)
        {
            using (Graphics gr = this.CreateGraphics())
            {
                string str = string.Empty;
                
                if (IsShowValue)
                {
                    str = LabelText + string.Format("{0}", this.Value);
                }
                else
                {
                    str = LabelText;
                }
                
                LinearGradientBrush brBG = new LinearGradientBrush(e.ClipRectangle, Color.DarkBlue, Color.DarkBlue, LinearGradientMode.Horizontal);
                e.Graphics.FillRectangle(brBG, e.ClipRectangle.X, e.ClipRectangle.Y, e.ClipRectangle.Width * this.Value / this.Maximum, e.ClipRectangle.Height);
                e.Graphics.DrawString(str, SystemFonts.DefaultFont, Brushes.Black , new PointF(this.Width / 2 - (gr.MeasureString(str, SystemFonts.DefaultFont).Width / 2.0F), this.Height / 2 - (gr.MeasureString(str, SystemFonts.DefaultFont).Height / 2.0F)));
            }
        }
    }
}
